package com.yejiali.backend.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yejiali.backend.common.annotaion.SystemLog;
import com.yejiali.backend.common.httpcode.HttpCodeEnum;
import com.yejiali.backend.entity.User;
import com.yejiali.backend.entity.vo.UserInfoVO;
import com.yejiali.backend.mapper.UserMapper;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.UserService;

import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户表(User)表服务实现类
 *
 * @author yejiali
 * @since 2025-05-14 14:52:39
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

 /**
  * 修改用户信息
  * @param user
  * @return
  */
 @Override
 @SystemLog("修改用户信息")
 public Response update(User user) {
  //判断是否存在
  User preUser = this.getById(user.getId());
  //不存在
  if(Objects.isNull(preUser)){
   return Response.error(HttpCodeEnum.USER_NOT_FOUND.getCode(),"用户不存在");
  }
  //修改
  user.setUpdateTime(DateTime.now());
  this.updateById(user);
  return Response.success("修改成功");

 }

 /**
  * 获取用户信息
  * @param userId
  * @return
  */
 @Override
 @SystemLog("查询用户信息")
 public Response getUserInfo(Integer userId) {
  //判断是否存在
  User user = this.getById(userId);
  //不存在
  if(Objects.isNull(user)){
   return Response.error(400,"用户不存在");
  }
  //封装
  UserInfoVO userInfoVO = BeanUtil.copyProperties(user, UserInfoVO.class);
  return Response.success(userInfoVO);

   }

    /**
     * 更新登录时间
     * @param userId
     */
    @Override
    public void updateLoginTime(Long userId) {
     User user = getById(userId);
     user.setLastLogin(DateTime.now());
     this.updateById(user);

    }
    /**
     * 更新用户头像
     *
     */
     public void updateAvatar(String avatar, Long userId) {
     User user = getById(userId);
     user.setAvatar(avatar);
     this.updateById(user);
 }
}


